from django.http import HttpResponse
from config.redis_conf import r
import logging


def transmit_dh2hyr(request):
    # 设置是否走恒易融转发
    is_hyr = request.GET.get("is_hyr")
    r.set('is_hyr_status', is_hyr)
    if r.get('is_hyr_status') in ('0', '1'):
        res = '恒易融转发参数设置成功'
    else:
        res = '恒易融转发状态参数错误'
    return HttpResponse(res)


def setRepayStatus(request):
    '''设置正常月还返回失败异常'''
    borrowid = request.GET.get('borrowid')
    status = request.GET.get('status')
    logging.info('接收参数borrowid:{0}，status:{1}'.format(borrowid, status))
    if borrowid == None or status == None:
        mes = "必填参数不能为空"
    elif borrowid not in r.hkeys('fund_fail_list'):
        r.hset('fund_fail_list', borrowid, status)
        mes = '正常月还失败前置条件，设置成功'
    else:
        mes = '合同在失败列表已存在'
    return HttpResponse(mes)


def setOverduStatus(request):
    '''设置逾期划扣返回失败异常'''
    borrowid = request.GET.get('borrowid')
    status = request.GET.get('status')
    logging.info('接收参数borrowid:{0}，status:{1}'.format(borrowid, status))
    if borrowid not in r.hkeys('overdue_fail_list'):
        r.hset('overdue_fail_list', borrowid, status)
        mes = '逾期划扣失败前置条件，设置成功'
    else:
        mes = '合同在失败列表已存在'
    return HttpResponse(mes)


def setJqStatus(request):
    '''设置结清失败列表'''
    contnum = request.GET.get('contnum')
    status = request.GET.get('status')
    logging.info('接收参数contnum:{0}，status:{1}'.format(contnum, status))
    if contnum not in r.hkeys('settle_fail_list'):
        r.hset('settle_fail_list', contnum, status)
        mes = '结清失败前置，设置成功'
    else:
        mes = '合同在失败列表已存在'
    return HttpResponse(mes)


def setHhStatus(request):
    '''设置划回失败列表'''
    accountNumber = request.GET.get('accountNumber')
    status = request.GET.get('status')
    logging.info('接收参数accountNumber:{0}，status:{1}'.format(accountNumber, status))
    if accountNumber not in r.hkeys('settle_fail_list'):
        r.hset('settle_fail_list', accountNumber, status)
        mes = '划回失败前置，设置成功'
    else:
        mes = '合同在失败列表已存在'
    return HttpResponse(mes)
